package cn.summit.core.controller;

import cn.summit.core.authentication.MyConstant;
import cn.summit.core.mobile.SmsSend;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author summit
 * @since 2020/3/4 21:26
 */
@Controller
public class MobileController {

    /**
     * 前往手机验证码登录页
     *
     * @return
     */
    @RequestMapping(MyConstant.mobile_page_url)
    public String toMobilePage() {
        return "login-mobile";
    }

    @Autowired
    private SmsSend smsSend;

    /**
     * 发送手机验证码
     *
     * @return
     */
    @ResponseBody //响应json字符串
    @RequestMapping(MyConstant.mobile_from_url)
    public String smsCode(HttpServletRequest request) {
        // 1. 生成一个手机验证码
        String code = RandomStringUtils.randomNumeric(4);
        // 2. 将手机验证码保存到session中
        request.getSession().setAttribute(MyConstant.SESSION_KEY_MOBILE_CODE, code);
        // 3. 发送验证码到用户手机上
        String mobile = request.getParameter("mobile");
        smsSend.sendSms(mobile, code);
        return "发送成功";
    }

}
